<template>
  <div class="app-container">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <VerifyType v-model="filter.approve_key" />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <el-input v-model="filter.forms" placeholder="描述" size="small" clearable />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" style="width: 300px">
          <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
          <el-button type="primary" size="small" @click="getList">查 询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="审批类型" width="120px" prop="value" />
      <el-table-column label="状态" align="center" width="90px" :formatter="states" />
      <el-table-column label="申请时间" align="center" width="140" prop="create_time" />
      <el-table-column label="描述" min-width="260px">
        <template slot-scope="scope">
          <span v-html="des(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleDialog({...row, ...{dTitle: '我的审批', dType: 'edit', dField: dField}})">详 情</el-dropdown-item>
              <el-dropdown-item @click.native="handleAgain(row)">再次提交</el-dropdown-item>
              <el-dropdown-item :disabled="row.state === 2 ? false : true" @click.native="handleEdit(row)">撤 销</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="filter.pageAll>0"
      :total="filter.pageAll"
      :page.sync="filter.pageCurrent"
      :limit.sync="filter.pageNumber"
      @pagination="getList"
    />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    <Fabao ref="fabao" :info="dialog" :get-data="getList" />
    <Wuliao ref="wuliao" :info="dialog" :get-data="getList" />
    <Shigongtu ref="shigongtu" :info="dialog" :get-data="getList" />
    <Jungongtu ref="jungongtu" :info="dialog" :get-data="getList" />
    <Baogao ref="baogao" :info="dialog" :get-data="getList" />
    <Jihua ref="jihua" :info="dialog" :get-data="getList" />
    <Warehousing ref="warehousing" :info="dialog" :get-data="getList" />
    <Fund ref="fund" :info="dialog" :get-data="getList" />
    <Finish ref="finish" :info="dialog" :get-data="getList" />
    <Accept ref="accept" :info="dialog" :get-data="getList" />
    <Warranty ref="warranty" :info="dialog" :get-data="getList" />
    <Supplier ref="supplier" :info="dialog" :get-data="getList" />
    <Inquiry ref="inquiry" :info="dialog" :get-data="getList" />
    <EO ref="eo" :info="dialog" :get-data="getList" />
    <Reduction ref="reduction" :info="dialog" :get-data="getList" />
    <Additional ref="additional" :info="dialog" :get-data="getList" />
    <Deduction ref="deduction" :info="dialog" :get-data="getList" />
    <Payment ref="payment" :info="dialog" :get-data="getList" />
    <Expend ref="expend" :info="dialog" :get-data="getList" />
    <Voucher ref="voucher" :info="dialog" :get-data="getList" />
    <Temporary ref="temporary" :info="dialog" :get-data="getList" />
    <Reimbursement ref="reimbursement" :info="dialog" :get-data="getList" />
    <Invoicing ref="invoicing" :info="dialog" :get-data="getList" />
    <Leave ref="leave" :info="dialog" :get-data="getList" />
    <Travel ref="travel" :info="dialog" :get-data="getList" />
    <Out ref="out" :info="dialog" :get-data="getList" />
    <Overtime ref="overtime" :info="dialog" :get-data="getList" />
    <Cart ref="cart" :info="dialog" :get-data="getList" />
    <Dormitory ref="dormitory" :info="dialog" :get-data="getList" />
    <Seal ref="seal" :info="dialog" :get-data="getList" />
    <Loss ref="loss" :info="dialog" :get-data="getList" />
    <Become ref="become" :info="dialog" :get-data="getList" />
    <Quit ref="quit" :info="dialog" :get-data="getList" />
  </div>
</template>

<script>
import { applys, revoke } from '@/api/approval/approve'
import VerifyType from '@/myComponents/VerifyType'
import Dialog from './components/my-dialog'
import page from '@/utils/page'
import approval from './util/approval'

export default {
  name: '',
  components: { VerifyType, Dialog },
  extends: page,
  mixins: [approval],
  props: {
    reqList: {
      default: applys,
      type: Function
    }
  },
  data() {
    return {
      dField: ['id', 'lap_id', 'approve_key', 'su_id', 'des', 'forms', 'files', 'tables', 'state', 'create_time']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleEdit(e) {
      this.$confirm('此操作将撤销审批流程, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        revoke({ id: e.id }).then(response => {
          this.getList()
          this.$message.success('编辑成功')
        }).catch(err => {
          this.$message.error(err.msg)
        })
      })
    },
    handleAgain(e) {
      if (this.approveKey[e.approve_key]) {
        const indexs = this.approveKey[e.approve_key].split('#')
        const approve_key = indexs[0]
        const index = parseInt(indexs[1])
        let list = []
        if (approve_key === 'engineerings') {
          list = this.engineerings
        } else if (approve_key === 'purchases') {
          list = this.purchases
        } else if (approve_key === 'finances') {
          list = this.finances
        } else if (approve_key === 'administrations') {
          list = this.administrations
        }
        const o = list[index]
        if (o) {
          this.handleDialog({ ...{ id: e.id }, ...{ dTitle: o.dialog.dTitle, dType: 'again', dField: o.dialog.dField, dRef: o.key }})
        }
      }
    },
    states(e) {
      let s = ''
      switch (e.state) {
        case 0:
          s = '已驳回'
          break
        case 1:
          s = '已通过'
          break
        case 2:
          s = '进行中'
          break
        case 3:
          s = '已撤销'
          break
        default:
          break
      }
      return s
    },
    des(e) {
      if (e.forms) {
        try {
          const forms = JSON.parse(e.forms)
          let sd = ''
          const len = forms.length
          for (let i = 0; i < len; i++) {
            sd += forms[i].t + ': ' + forms[i].v + '<br/>'
          }
          return sd
        } catch (error) {
          return e.des
        }
      } else {
        return e.des
      }
    }
  }
}
</script>
